import entity.Employee;
import org.junit.Test;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

/**
 * Created by VincentZee on 2019/3/6
 */
public class TestStreamAPI02 {
    List<Employee> emps = Arrays.asList(
            new Employee(102, "李四", 59, 6666.66, Employee.Status.FREE),
            new Employee(101, "张三", 18, 9999.99, Employee.Status.FREE),
            new Employee(103, "王五", 28, 3333.33, Employee.Status.VOCATION),
            new Employee(104, "赵六", 8, 7777.77, Employee.Status.BUSY),
            new Employee(104, "赵六", 8, 7777.77, Employee.Status.FREE),
            new Employee(104, "赵六", 8, 7777.77, Employee.Status.FREE),
            new Employee(105, "田七", 38, 5555.55, Employee.Status.BUSY)
    );

    //3. 终止操作
	/*
		allMatch——检查是否匹配所有元素
		anyMatch——检查是否至少匹配一个元素
		noneMatch——检查是否没有匹配的元素
		findFirst——返回第一个元素
		findAny——返回当前流中的任意元素
		count——返回流中元素的总个数
		max——返回流中最大值
		min——返回流中最小值
	 */
	@Test
    public void test01(){
        boolean b1 = emps.stream().allMatch((e)->e.getStatus().equals(Employee.Status.BUSY));
        System.out.println(b1);

        boolean b2 = emps.stream().anyMatch((e)->e.getStatus().equals(Employee.Status.BUSY));
        System.out.println(b2);

        boolean b3 = emps.stream().noneMatch((e)->e.getStatus().equals(Employee.Status.BUSY));
        System.out.println(b3);
    }

    @Test
    public void test02(){
	    Optional<Employee> optionalEmployee = emps.stream().sorted(Comparator.comparing(Employee::getSalary)).findFirst();
        System.out.println(optionalEmployee.get());

        Optional<Employee> optionalEmployee1 = emps.stream().filter((e)->e.getStatus().equals(Employee.Status.FREE)).findAny();
        System.out.println(optionalEmployee1.get());
    }

    @Test
    public void test03(){
	    long count = emps.stream().filter((e)->e.getStatus().equals(Employee.Status.FREE)).count();
        System.out.println(count);

        Optional<Double> optionalDouble = emps.stream().map(Employee::getSalary).max(Double::compare);
        System.out.println(optionalDouble.get());

        Optional<Employee> employeeOptional = emps.stream().min(Comparator.comparing(Employee::getSalary));
        System.out.println(employeeOptional.get());
    }

    @Test
    public void test04(){
	    Stream<Employee> employeeStream = emps.stream().filter((e)->e.getStatus().equals(Employee.Status.FREE));
	    long count = employeeStream.count();
        System.out.println(count);

        // 注意：流进行了终止操作后，不能再次使用（此处报错：java.lang.IllegalStateException: stream has already been operated upon or closed）
        Optional<Double> optionalDouble = employeeStream.map(Employee::getSalary).max(Double::compare);
        System.out.println(optionalDouble);
    }
}
